var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "js/general_functions.js");
document.getElementsByTagName("head")[0].appendChild(script);
	
function login_submit(){
    var cmnd = document.getElementById("cmnd");
    var username = document.getElementById("username");
    var password = document.getElementById("password");
    var rememberTS = document.getElementById("rememberTS").checked;
    var rememberQT = document.getElementById("rememberQT").checked;
    if (document.getElementById("thisinh").style.display == "block"){
        if ( cmnd.value.length == 0 ) {
            alert("Chưa nhập CMND !");
            return;
        }
        if ( isNaN(cmnd.value) || cmnd.value.length != 9 ){
            alert("CMND không hợp lệ !");
            return;
        }
    } else {
        if ( username.value.length == 0 ) {
            alert("Chưa nhập username !");
            return;
        }
        if ( password.value.length == 0 ){
            alert("Chưa nhập password !");
            return;
        }
    }

    xmlhttp = createXMLHttpRequest();
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            var res = xmlhttp.responseText.replace(/[^0-9]/g, '');
            if (res == "1")
                window.open("index.jsp", '_self', false);
            else
                document.getElementById("login_failed").innerHTML = "Tài khoản không đúng<br/>Đăng nhập thất bại";
        }
    }
    var serverURL = "login_xuly.jsp?";
    if (document.getElementById("thisinh").style.display == "block"){
        serverURL += "cmnd=" +  encodeURI(cmnd.value);
        if (rememberTS)
            serverURL += "&remember=1";
    }
    else {
        serverURL += "username=" +  encodeURI(username.value);
        serverURL += "&password=" +  encodeURI(password.value);
        if (rememberQT)
            serverURL += "&remember=1";
    }
    serverURL += "&t=" + (new Date()).getTime();
    xmlhttp.open("GET",serverURL,true);
    xmlhttp.send();
}

function thisinh(){
    document.getElementById("thisinh").style.display = "block";
    document.getElementById("quantri").style.display = "none";
    document.getElementById("typeTS").style.color = "#0c3";
    document.getElementById("typeQT").style.color = "";
}

function quantri(){
    document.getElementById("thisinh").style.display = "none";
    document.getElementById("quantri").style.display = "block";
    document.getElementById("typeQT").style.color = "#0c3";
    document.getElementById("typeTS").style.color = "";
}
